package javaer.starter.core.generator;

import freemarker.template.TemplateException;
import javaer.starter.core.BuildContext;
import javaer.starter.core.handler.DynamicTemplateHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Collections;
import java.util.Optional;

/**
 * @author yangzhen
 * @date 2023/6/15 14:30
 * @since 1.0.0
 */
@Component
public class ModuleGenerator implements IGenerator {

    @Resource
    private DynamicTemplateHandler dynamicTemplateHandler;

    @Override
    public void generate(BuildContext buildContext) throws TemplateException, IOException {
        Optional.ofNullable(buildContext.getChildModules()).orElse(Collections.emptyList()).forEach(v -> {
            dynamicTemplateHandler.handle(initModuleContext(buildContext, v));
        });
    }
}
